<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/bean/Usuario.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/bean/Nivel.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/dao/UsuarioDAO.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/controller/AbstractController.php'; ?>
<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/JuventudeSlz/utils/System.php'; ?>

<?php

/**
 * Description of UsuarioController
 *
 * @author JoasRamos
 */
class ControleUsuario extends AbstractController{

    var $dao, $bean, $nivel;

    public function __construct($acao) {
        $this->dao = new UsuarioDAO();
        $this->bean = new Usuario();
        parent::__construct($acao);
    }

    public function validar() {
        $user = $_POST['inUser'];
        $pass = $_POST['inPass'];
        $nivel = new Nivel();

        $this->bean = $this->dao->findUser($user, $pass);

        if ($this->bean->getId() == 0) {
            System::showViewParameter("../view/erro_page", array("erro_desc"), array("no-user-find"));
        } else {
            if (System::destroySessionDefault()) {
                if (session_start()) {
                    $nivel = $this->bean->getNivel();
                    $_SESSION['current_user'] = array("id" => $this->bean->getId(),
                        "nome" => $this->bean->getNome(),
                        "nivel" => $nivel->getNivel(),
                        "desc_nivel" => $nivel->getDescricao());
                    System::showSimpleView("../view/painel_admin");
                } else {
                    System::showViewParameter("../view/erro_page", array("erro_desc"), array("no-init-session"));
                }
            } else {
                System::showViewParameter("../view/erro_page", array("erro_desc"), array("no-destroy-session"));
            }
        }
    }

    public function fecharSessao() {
        session_start();
        session_destroy();
        //
        System::createSessionDefault();
        System::showSimpleView("../view/homepage");
    }

    public function personalizeAction($action) {
        if ($action == md5("valide")) {
            $this->validar();
        } else if ($action == md5("out")) {
            $this->fecharSessao();
        }
    }

    public function retrieve($id) {
        
    }

    public function create() {
        
    }

    public function delete() {
        
    }

}

if (isset($_REQUEST["code_action"])) {
    new ControleUsuario($_REQUEST["code_action"]);
}
?>
